<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use App\Model\BaseModel;
class SchoolKeepModel extends BaseModel
{
    protected $table='school_keep';
    public $timestamps = false;
    protected $guarded = [];

    public function schoolFocusNum($sid)
    {
       $res['count'] =  SchoolKeepModel::where(['sid' => $sid]) ->count();

        if($res){//成功！
            return ['code' => true,'data' => $res ];
        }else{
            return ['code' => false,'data' => '数据为空'];
        }
    }

    public function SaveFocusSchool($uuid,$sid)
    {
        $res =   SchoolKeepModel::create([
            'uid' => $uuid,
            'sid' => $sid,
            'created_at' => date('Y:m:d H:i:s')
        ]);

        if($res){

            //        更新用户表
            $user = UserModel::find($uuid);
            $user['school_num'] = $user ->school_num + 1;
            $user -> save();

            return ['code' => true, 'data' => 1];//成功
        }else{
            return ['code' => true, 'data' => 0];//失败
        }

    }

    public function DeletedFocusSchool($uuid,$sid)
    {
        $res =   SchoolKeepModel::where(['uid' => $uuid,'sid' =>$sid]) ->delete();

        if($res){

            //        更新用户表
            $user = UserModel::find($uuid);
            $user['school_num'] = $user ->school_num - 1;
            $user -> save();

            return ['code' => true, 'data' => 1];//成功
        }else{
            return ['code' => true, 'data' => 0];//失败
        }

    }

}
